package com.yim.hql;

import java.lang.reflect.Field;

/**
 * 根据实体构建hql语句
 * @author lxl
 * @createTime 2013-05-10 16:00
 * @version 1.0
 */
@SuppressWarnings("all")
public class BuildHqlUtil {
	/**
	 * 根据实体生成对应的hql语句，对象中具有值的属性将成为查询的限制条件部分<br/>
	 * 其中，对于String类型的属性是 like 判断的，以其开头的：like '[value]%'
	 * @param entity
	 * @return
	 */
	public static String buildSelectStr(Object entity) {
		return buildSelectStr(entity, null);
	}

	/**
	 * 根据实体生成有字段限制的hql语句，对象中具有值的属性将成为查询的限制条件部分<br/>
	 * 其中，对于String类型的属性是 like 判断的，以其开头的：like '[value]%'
	 * @param entity
	 * @param queryFields 数组形式的查询字段：select xxx,xxx ...
	 * @return
	 */
	public static String buildSelectStr(Object entity, String[] queryFields) {
		if (entity == null) {
			try {
				throw new Exception("实体不能为null");
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}
		Class clazz = entity.getClass();
		String hqlStr = "";
		try {
			Field[] fields = clazz.getDeclaredFields();
			Field.setAccessible(fields, true);
			hqlStr = getFieldMsg(fields, queryFields) + getWhereMsg(entity, fields, queryFields);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return hqlStr;
	}
	
	/**
	 * 构建查询字段
	 * @param fields
	 * @param queryFields
	 * @return
	 * @throws Exception
	 */
	private static String getFieldMsg(Field[] fields, String[] queryFields)
			throws Exception {
		String selectStr = "";
		int i, j;
		if (queryFields != null && queryFields.length != 0) {
			selectStr = "select ";
			for (i = 0; i < queryFields.length; i++) {
				if (!"".equals(queryFields[i])) {
					boolean flag = false;
					for (j = 0; j < fields.length; j++) {
						if (queryFields[i].equals(fields[j].getName())) {
							flag = true;
						}
					}
					if (flag) {
						selectStr += queryFields[i] + ",";
					} else {
						throw new Exception("实体中不存在该字段：" + queryFields[i]);
					}
				}
			}
			if ("select ".equals(selectStr)) {
				selectStr = "";
			} else {
				selectStr = selectStr.substring(0, selectStr.length() - 1)
						+ " ";
			}
		}
		return selectStr;
	}
	
	/**
	 * 构建查询条件
	 * 
	 * @param entity
	 * @param fields
	 * @param queryFields
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	private static String getWhereMsg(Object entity, Field[] fields, String[] queryFields)
			throws IllegalArgumentException, IllegalAccessException {
		String whereStr = "from " + entity.getClass().getSimpleName() + " where 1=1";
		for (int i = 0; i < fields.length; i++) {
			if (fields[i].get(entity) == null || fields[i].get(entity).toString().trim().length() == 0) {
				continue;
			}
			String value = fields[i].get(entity).toString();
			String field = fields[i].getName();
			if (isNum(fields[i].getType().getName())) {
				whereStr += " and " + field + "=" + value;
			} else {
				whereStr += " and " + field + " like '" + value + "%'";
			}
		}
		return whereStr;
	}

	/**
	 * 判断是否为Integer，Double，Float等数字类型，注意都是封装类型
	 * @param fieldName
	 * @return
	 */
	private static boolean isNum(String fieldName) {
		boolean flag = false;
		if (fieldName.endsWith(".Integer")
				|| fieldName.endsWith(".Double")
				|| fieldName.endsWith(".Float")
				|| fieldName.endsWith(".Long")
				|| fieldName.endsWith(".Number")
				|| fieldName.endsWith(".BigDecimal")) {
			flag = true;
		}
		return flag;
	}

}